package com.gs.view.riepilogo;

import java.sql.SQLException;
import java.util.Vector;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.gs.R;
import com.gs.business.cache.CacheCategorie;
import com.gs.domain.CatSpese;
import com.gs.view.impostazioni.CategorieView;

public class ListaCategorie extends comi.gsi.view.MyBaseActivity {

	@Override
	protected int getIdLayout() {
		return R.layout.listattivita;
	}

	@Override
	protected void myOnCreate(final Bundle savedInstanceState) {

		Vector<CatSpese> cats = null;
		try {
			cats = CacheCategorie.getSingleton().getVettoreCategorie();
		}
		catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		catch (InstantiationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		catch (IllegalAccessException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		ArrayAdapter<CatSpese> adapter = new ArrayAdapter<CatSpese>(this, R.layout.rowcategoria, cats.toArray(new CatSpese[cats.size()])) {
			@Override
			public View getView(final int position, View convertView, final ViewGroup parent) {
				ViewHolder viewHolder = null;
				if (convertView == null) {
					final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
					convertView = inflater.inflate(R.layout.rowcategoria, null);
					viewHolder = new ViewHolder();
					convertView.setTag(viewHolder);
				} else {
					viewHolder = (ViewHolder) convertView.getTag();
				}
				final CatSpese categoria = getItem(position);
				getListNome(viewHolder, convertView).setText(categoria.getNomeEntita() != null ? categoria.getNomeEntita() : "NO");
				getListDescrizione(viewHolder, convertView).setText(categoria.getdescrizione() != null ? categoria.getdescrizione() : "NO");
				getListGruppo(viewHolder, convertView).setText(categoria.getGruppi() != null ? categoria.getGruppi().getNomeEntita() : "NO");
				convertView.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(final View v) {

						final Intent i = new Intent(ListaCategorie.this, CategorieView.class);
						i.putExtra("idCategoria", categoria.getidCategoria());
						try {
							startActivity(i);
						} catch (final Exception e) {
							Log.e("boh", e.getMessage());
						}

					}
				});

				return convertView;
			}
		};

		ListView list = (ListView) findViewById(R.id.listView1);
		list.setAdapter(adapter);

	}

	private TextView getListNome(final ViewHolder viewHolder, final View convertView) {
		if (viewHolder.listNomeTrs == null && convertView != null) {
			viewHolder.listNomeTrs = (TextView) convertView.findViewById(R.id.listaCatNome);
		}
		return viewHolder.listNomeTrs;
	}

	private TextView getListDescrizione(final ViewHolder viewHolder, final View convertView) {
		if (viewHolder.listDescrTrs == null && convertView != null) {
			viewHolder.listDescrTrs = (TextView) convertView.findViewById(R.id.listaCatDescr);
		}
		return viewHolder.listDescrTrs;
	}

	private TextView getListGruppo(final ViewHolder viewHolder, final View convertView) {
		if (viewHolder.listGruppoTrs == null && convertView != null) {
			viewHolder.listGruppoTrs = (TextView) convertView.findViewById(R.id.listaCatGruppo);
		}
		return viewHolder.listGruppoTrs;
	}

	private static class ViewHolder {
		protected TextView listGruppoTrs;
		public TextView listNomeTrs;
		public TextView listDescrTrs;
	}
}
